package io.seata.samples.tcc.springboot.dubbo.service.impl;

import io.seata.samples.tcc.springboot.dubbo.dao.UserMapper;
import io.seata.samples.tcc.springboot.dubbo.model.User;
import io.seata.samples.tcc.springboot.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(version = "1.0.0", group = "tcc")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User save(User user) {
        if(user.getId() == null || "".equals(user.getId())) {
            userMapper.insert(user);
        } else {
            userMapper.updateByPrimaryKeySelective(user);
        }

        return user;
    }

    @Override
    public boolean delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public User findById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> list() {
        return userMapper.selectAll();
    }
}
